package com.ziyun.pop.web.modules.order.controller;

import com.utility.http.HttpUtils;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.order.annotation.OrderLog;
import com.ziyun.pop.modules.order.entity.OrderInfoEntity;
import com.ziyun.pop.modules.order.service.OrderInfoService;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 订单基础信息表（商城订单）
 *
 * @author guizhao
 * @date 2018-08-03 10:40:47
 */
@RestController
@RequestMapping("order/orderinfo/mall")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    @Value("${erpApi}")
    private String erpApi ;

    /**
     * 商城订单列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        // 查询供应商id
        params.put("supplierId", UserUtils.getCurentUser().getSellerId());
        return orderInfoService.queryPageMallOrderList(params);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{orderId}")
    public R info(@PathVariable("orderId") Long orderId){
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/order/orderinfo/mall/queryOrderinfo";
        paramBodyMap.put("orderId",orderId);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("order:orderinfo:save")
    public R save(@RequestBody OrderInfoEntity orderInfo){
        orderInfo.setSupplierId(UserUtils.getCurentUser().getSellerId().intValue());
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/order/orderinfo/mall/save";
        paramBodyMap.put("orderInfo",JsonUtils.object2String(orderInfo));
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("order:orderinfo:update")
    public R update(@RequestBody OrderInfoEntity orderInfo){
        ValidatorUtils.validateEntity(orderInfo);
        orderInfo.setSupplierId(UserUtils.getCurentUser().getSellerId().intValue());
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/order/orderinfo/mall/update";
        paramBodyMap.put("orderInfo",JsonUtils.object2String(orderInfo));
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("order:orderinfo:delete")
    public R delete(@RequestBody Long[] orderIds){
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/order/orderinfo/mall/delete";
        paramBodyMap.put("orderIds",JsonUtils.object2String(orderIds));
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

     /**
     * 订单备注
     * @param orderId 订单号
     * @param remarks 备注
     * @return
     */
    @RequestMapping("/orderRemarks")
    public R orderRemarks(@RequestParam("orderId") String orderId, @RequestParam("remarks") String remarks){
        // 当前登陆用户ID
        Long userId = UserUtils.getCurentUser().getUserId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/order/orderinfo/mall/orderRemarks";
        paramBodyMap.put("orderId",orderId);
        paramBodyMap.put("remarks",remarks);
        paramBodyMap.put("userId",userId);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 订单作废
     * @param orderId 订单号
     * @param remarks 备注
     * @return
     */
    @OrderLog(desc = "订单作废",orderNo = false)
    @RequestMapping("/orderInvalid")
    public R orderInvalid(@RequestParam("orderId") String orderId, @RequestParam("remarks") String remarks){
        // 当前登陆用户ID
        Long userId = UserUtils.getCurentUser().getUserId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/order/orderinfo/orderInvalid";
        paramBodyMap.put("orderId",orderId);
        paramBodyMap.put("remarks",remarks);
        paramBodyMap.put("userId",userId);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 订单完成
     * @param orderId 订单号
     * @param remarks 备注
     * @return
     */
    @RequestMapping("/orderFinish")
    public R orderFinish(@RequestParam("orderId") String orderId, @RequestParam("remarks") String remarks){
             // 当前登陆用户ID
            Long userId = UserUtils.getCurentUser().getUserId();
            String userName=UserUtils.getCurentUser().getUsername();
            Map<String, Object> paramBodyMap = new HashMap<String, Object>();
            String url = erpApi + "/order/orderinfo/mall/orderFinish";
            paramBodyMap.put("orderId",orderId);
            paramBodyMap.put("remarks",remarks);
            paramBodyMap.put("userId",userId);
            paramBodyMap.put("userName",userName);
            String response =  HttpUtils.doPost(url,paramBodyMap);
            R rs = JsonUtils.string2Object(response, R.class);
            return rs;
    }

    /*
     * 订单支付
     * @param orderId  订单号
     * @param formdata 表单数据
     * @return
     */
    @OrderLog(desc = "订单支付",orderNo = false)
    @RequestMapping("/orderPay")
    public R orderPay(@RequestParam("orderId") String orderId, @RequestParam("formdata") String formdata){
       Long userId=UserUtils.getCurentUser().getUserId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/order/orderinfo/mall/orderPay";
        paramBodyMap.put("orderId",orderId);
        paramBodyMap.put("formdata",formdata);
        paramBodyMap.put("userId",userId);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
      }

    /**
     * 订单调价
     * @param orderId 订单号
     * @param discount 调价
     * @return
     */
    @OrderLog(desc = "订单调价",orderNo = false)
    @RequestMapping("/orderDiscountAudit")
    public R orderDiscountAudit(@RequestParam("orderId") String orderId, @RequestParam("discount") String discount,
                                @RequestParam("remark") String remark){
        if (StringUtils.isBlank(orderId) || StringUtils.isBlank(discount) || StringUtils.isBlank(remark)){
            return R.error("请求参数有误！");
        }
        // 当前登陆用户ID
        Long userId = UserUtils.getCurentUser().getUserId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/order/orderinfo/mall/orderDiscountAudit";
        paramBodyMap.put("orderId",orderId);
        paramBodyMap.put("discount",discount);
        paramBodyMap.put("remark",remark);
        paramBodyMap.put("userId",userId);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    @RequestMapping("/sendLogisticsInfo/{orderId}")
    public R sendLogisticsInfo(@PathVariable("orderId") String orderId){
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/order/orderinfo/mall/sendLogisticsInfo";
        paramBodyMap.put("orderId",orderId);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 转到门店生产
     * @param orderId
     * @return
     */
    @RequestMapping("/produce")
    public R produce(@RequestParam("orderId") String orderId){
        return orderInfoService.produce(orderId);
    }

    /**
     * 打印快递单
     * @param orderId
     * @return
     */
    @RequestMapping("/printGoods")
    public R printGoods(@RequestParam("orderId") String orderId){
        return orderInfoService.printGoods(orderId);
    }

}
